﻿using System;
using System.Net;
using CuteRibs.D3Armory.Resources;
using CuteRibs.D3Armory.ViewModels;

namespace CuteRibs.D3Armory.Views
{
	public partial class RealmStatusPage : PageBase
	{
		private RealmStatusPageViewModel myViewModel;

		public RealmStatusPageViewModel MyViewModel
		{
			get
			{
				if (this.myViewModel == null)
				{
					this.myViewModel = (RealmStatusPageViewModel)this.DataContext;
				}

				return this.myViewModel;
			}
		}

		public RealmStatusPage()
		{
			InitializeComponent();

			this.Loaded += delegate
			{
				this.AppButtons[0].Text = AppRes.btnRefresh;
				this.RefreshStatus();
			};
		}

		private async void RefreshStatus()
		{
			this.MyViewModel.ShowProgressBar = true;

			try
			{
				this.MyViewModel.Realms = await App.GetRealmStatus();
			}
			catch (Exception ex)
			{
				if (ex is WebException)
				{
					App.ShowAlert(AppRes.MsgDataAccessError);
				}
				else
				{
					App.ShowError(ex);
				}
			}

			this.MyViewModel.ShowProgressBar = false;
		}

		private void btnRefresh_Click(object sender, EventArgs e)
		{
			this.RefreshStatus();
		}
	}
}